<?php
declare (strict_types=1);

namespace app\common\model;

use app\common\model\Product as ProductModel;

class UserProductView extends Base
{
    protected $name = 'user_product_view';

    public static function foot($user) :array
    {
        try {
            $pids = UserProductView::where('uid', $user->id)
                ->field('GROUP_CONCAT(pid) as pids, create_date')
                ->group('create_date')
                ->order('create_date', 'desc')
                ->select()->toArray();
            $pids = array_column($pids, 'pids', 'create_date');

            $list = [];
            foreach ($pids as $date => $pid) {
                $list[$date]['date'] = $date == date("Y-m-d") ? '今天' : $date;
                $list[$date]['list'] = ProductModel::field('id,title,price,vip_price,icon')
                    ->whereIn('id', $pid)
                    ->select();
            }
            $list = array_values($list);
            return ['code' => 1, 'msg' => 'success', 'data' => $list];
        }catch (\Exception $e){
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
